package com.assessment.servermanager.service.impl;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.secure.BCrypt;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import com.assessment.servermanager.commen.constant.Constants;
import com.assessment.servermanager.commen.entity.model.LoginUser;
import com.assessment.servermanager.commen.exception.ServiceException;
import com.assessment.servermanager.commen.exception.user.UserException;
import com.assessment.servermanager.commen.utils.MessageUtils;
import com.assessment.servermanager.entity.TbUser;
import com.assessment.servermanager.mapper.TbUserMapper;
import com.assessment.servermanager.service.ILoginService;
import com.assessment.servermanager.service.ITbUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private TbUserMapper userMapper;
    @Override
    public String login(String userName, String password, String code) {
        TbUser user = loadUserByUserName(userName);
        /*checkLogin(LoginType.PASSWORD, username, () -> !BCrypt.checkpw(password, user.getPassword()));*/
        user = userMapper.selectOne(new LambdaQueryWrapper<TbUser>().eq(TbUser::getUsername, user.getUsername()));
        checkLogin(user,password);
        LoginUser loginUser = buildLoginUser(user);
        SaLoginModel model = new SaLoginModel();
        model.setDevice("pc");
        StpUtil.login(loginUser.getLoginId(), model.setExtra("userId", loginUser.getUserId()));
        StpUtil.getTokenSession().set("loginUser", loginUser);
        return StpUtil.getTokenValue();
    }

    @Override
    public void logout() {
        try {
            StpUtil.logout();
        } catch (NotLoginException ignored) {
        }
    }

    private void checkLogin(TbUser user,String password){

        if(!user.getPassword().equals(password)){
            throw new ServiceException("用户名或密码错误");
        }
    }
    private TbUser loadUserByUserName(String userName){
        TbUser tbUser = userMapper.selectOne(new LambdaQueryWrapper<TbUser>().select(TbUser::getUsername)
                .eq(TbUser::getUsername, userName));
        if(ObjectUtils.isEmpty(tbUser)){
            log.info("登录用户：{}不存在",userName);
            throw new RuntimeException("用户不存在");
        }
        return tbUser;
    }

    /**
     * 构建登录用户
     */
    private LoginUser buildLoginUser(TbUser user) {
        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(user.getId());
        //loginUser.setDeptId(user.getDeptId());
        loginUser.setUsername(user.getUsername());
        //loginUser.setUserType(user.getUserType());
        //loginUser.setMenuPermission(permissionService.getMenuPermission(user));
        //loginUser.setRolePermission(permissionService.getRolePermission(user));
        //loginUser.setDeptName(ObjectUtil.isNull(user.getDept()) ? "" : user.getDept().getDeptName());
        //List<RoleDTO> roles = BeanUtil.copyToList(user.getRoles(), RoleDTO.class);
        //loginUser.setRoles(roles);
        return loginUser;
    }
}
